package com.example.qmjy.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.qmjy.R;
import com.example.qmjy.constant.UrlConstant;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

public class MyPagerAdapter extends PagerAdapter {
    private ArrayList<View> viewLists; // 定义一个存放视图的数组

    private Context context;

    // 网络资源
    String[] guideImages = {
            UrlConstant.SERVER_URL + "public/images/welcome1.jpg",
            UrlConstant.SERVER_URL + "Public/images/welcome2.jpg",
            UrlConstant.SERVER_URL + "Public/images/welcome3.jpg",
            UrlConstant.SERVER_URL + "Public/images/welcome4.jpg"
    };

    // 本地资源
    int[] guideImagesLocal = {
            R.mipmap.welcome1,
            R.mipmap.welcome2,
            R.mipmap.welcome3,
            R.mipmap.welcome4
    };

    public MyPagerAdapter() {
    }


    public MyPagerAdapter(ArrayList<View> viewLists) {
        super();
        this.viewLists = viewLists;
    }


    public MyPagerAdapter(ArrayList<View> viewLists, Context context) {
        super();
        this.context = context;
        this.viewLists = viewLists;
    }

    @Override
    public int getCount() {
        return viewLists.size();

    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // TODO R.id.guide_sdv替换为R.id.sdv_vp
        SimpleDraweeView simpleDraweeView = viewLists.get(position).findViewById(R.id.guide_sdv);
        // simpleDraweeView.setImageURI(guideImages[position]);
        simpleDraweeView.setImageResource(guideImagesLocal[position]);
        return viewLists.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(viewLists.get(position));
    }
}
